package jprint.jfx.http;

import jprint.base.http.AbstractRequestHandler;
import jprint.base.http.Http;
import jprint.base.update.Version;
import jprint.base.util.JsonUtil;
import jprint.jfx.UpdateAlert;

/**
 * @author zw-zheng
 * Created on 2021-02-06
 */
class VersionHandler extends AbstractRequestHandler {

    @Override public boolean handle(Http.HttpRequest request, Http.HttpResponse response) {
        String uri = request.uri();
        if (uri.equals("/api/version.json")) {
            response.setContentType(JSON_MIME_TYPE)
                .setStatus(200)
                .write(JsonUtil.toJsonString(Version.current()));
            return true;
        }
        if (uri.equals("/api/version-check-update")) {
            response.setContentType(JSON_MIME_TYPE)
                .setStatus(200)
                .write(SUCCESS_JSON);
            new UpdateAlert().show();
            return true;
        }
        return false;
    }
}
